//
//  JumpTool.m
//  Rail
//
//  Created by hychen on 2018/3/2.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "JumpTool.h"
#import "WatchVideoViewController.h"

@implementation JumpTool

#pragma mark - 拦截是否登录
+ (BOOL)isCheckUserLoginWithTarget:(id)target Action:(SEL)action Object:(id)object rootVC:(UIViewController *)rootVC {
    //登录拦截
    BOOL isLogin = [GlobalManage sharedManage].userInfoModel == nil?NO:YES;
    if (!isLogin) {
        __weak typeof(target) wself = target;
        LoginViewController *loginVc = [[LoginViewController alloc] initWithComeFrom:LoginComeFromTypePresentLogin];
        BaseNavigationController *nav =[[BaseNavigationController alloc] initWithRootViewController:loginVc];
        [nav setNavigationBarHidden:NO];
        
        [loginVc setBlockLoginSuccess:^{
            if (action && [wself respondsToSelector:action]){
                [wself performSelector:action withObject:object];
            }
        }];
        
        [rootVC presentViewController:nav animated:YES completion:nil];
        return NO;
    }
    return YES;
}

#pragma mark - 获取当前显示控制器
+ (UIViewController *)getCurrentViewController {
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    //app默认windowLevel是UIWindowLevelNormal，如果不是，找到UIWindowLevelNormal的
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow *tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    id nextResponder = nil;
    UIViewController *appRootVC = window.rootViewController;
    // 如果是present上来的appRootVC.presentedViewController 不为nil
    if (appRootVC.presentedViewController) {
        nextResponder = appRootVC.presentedViewController;
    }else{
        nextResponder = appRootVC;
    }
    
    return [[self class] searchNextVC:nextResponder];
}

// 循环遍历是否有多层present
+ (UIViewController *)searchNextVC:(id)nextResponder{
    if ([nextResponder isKindOfClass:[UITabBarController class]]){
        UITabBarController *tabbar = (UITabBarController *)nextResponder;
        
        nextResponder = tabbar.selectedViewController;
        
        //        UINavigationController *nav = (UINavigationController *)tabbar.viewControllers[tabbar.selectedIndex];
        //        nextResponder=nav.childViewControllers.lastObject;
        
    }
    else if ([nextResponder isKindOfClass:[UINavigationController class]]){
        UINavigationController *nav = (UINavigationController *)nextResponder;
        
        nextResponder = nav.visibleViewController;
        
        //        nextResponder = nav.childViewControllers.lastObject;
    }
    else{
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            UIViewController *vc = ((UIViewController *)nextResponder).presentedViewController;
            if (vc) {
                nextResponder = vc;
                //                nextResponder = [self searchNextVC:vc.childViewControllers.lastObject];
            }
            else{
                return nextResponder;
            }
        }
        else{
            return nextResponder = nil;
        }
    }
    
    return [self searchNextVC:nextResponder];
}

#pragma mark - 跳转
+ (void)gotoWebVcWithUrl:(NSString *)url viewController:(UIViewController *)vc {
    if ([url isKindOfClass:[NSString class]] && url.length > 0) {
        BaseWebController *webVc = [[BaseWebController alloc] init];
        webVc.url = url;
        [vc.navigationController pushViewController:webVc animated:YES];
    }
}

+ (void)gotoIndustyNewsDetail:(id)model {
    IndustryNewDetailViewController *vc = [IndustryNewDetailViewController new];
    if ([model isKindOfClass:[NewsModel class]]) {
        vc.model = model;
    }
    else if ([model isKindOfClass:[NewModel class]]) {
        vc.theNewModel = model;
    }
    [[BaseNavigationController rootNavigationController] pushViewController:vc animated:YES];
}

+ (void)gotoWatchVideoVc:(UIViewController *)vc deviceModel:(SelectVideoDeviceModel *)model{
    if (!model) {
        return;
    }
    WatchVideoViewController *videoVc = [[WatchVideoViewController alloc] initWithSelectDeviceModel:model];
    [vc.navigationController pushViewController:videoVc animated:YES];
}

+ (void)gotoWorkAreaDetailWithAreaId:(NSString *)areaId navTitle:(NSString *)navTitle vc:(UIViewController *)vc {
    FactoryDetailViewController *detailVc = [FactoryDetailViewController new];
    detailVc.areaid = areaId;
    detailVc.navTitle = navTitle;
    [vc.navigationController pushViewController:detailVc animated:YES];
}

+ (void)gotoLineDetailWithLineId:(NSString *)lineId vc:(UIViewController *)vc {
    LineDetailViewController *detailVc = [LineDetailViewController new];
    detailVc.lineId = lineId;
    [vc.navigationController pushViewController:detailVc animated:YES];
}

+ (void)gotoSectionDetailWithSectionId:(NSString *)sectionId vc:(UIViewController *)vc {
    if (![sectionId objectIsNotEmpty]) {
        return;
    }
    LineSectionDetailViewController *sectionVc = [LineSectionDetailViewController new];
    sectionVc.sectionId = sectionId;
    [vc.navigationController pushViewController:sectionVc animated:YES];
}

+ (void)gotoStationDetailWithStationId:(NSString *)stationId vc:(UIViewController *)vc {
    if (![stationId objectIsNotEmpty]) {
        return;
    }
    SiteDetailViewController *stationVc = [SiteDetailViewController new];
    stationVc.stationId = stationId;
    [vc.navigationController pushViewController:stationVc animated:YES];
}

+ (void)gotoWarnDetailVcWithModel:(WarnDetailModel *)model vc:(UIViewController *)vc {
    if ([model isKindOfClass:[WarnDetailModel class]]) {
        WarnDetailViewController *warnVc = [WarnDetailViewController new];
        warnVc.model = model;
        [vc.navigationController pushViewController:warnVc animated:YES];
    }
}
@end
